//获取应用实例
const app = getApp();

Page({
  data: {
    collectList: [],
    currtab: 0,
    swipertab: [{
      name: '淘宝收藏',
      index: 0
    }, {
      name: '京东收藏',
      index: 1
    }, {
      name: '拼多多收藏',
      index: 2
    }],
    noData: false
  },
  onLoad: function() {

  },

  onShow() {
    this.getDeviceInfo();
    this.getCollectList();
  },

  /**
  * @Explain：选项卡点击切换
  */
  tabSwitch: function (e) {
    var that = this
    if (this.data.currtab === e.target.dataset.current) {
      return false
    } else {
      that.setData({
        currtab: e.target.dataset.current
      })
    }
    
  },

  getDeviceInfo: function () {
    let that = this
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          deviceW: res.windowWidth,
          deviceH: res.windowHeight
        })
      }
    })
  },

  tabChange: function(e) {
    this.setData({
      currtab: e.detail.current
    })
    this.orderShow();
  },

  orderShow: function () {
    let that = this
    switch (this.data.currtab) {
      case 0:
        that.getCollectList();
        break
      case 1:
        that.getCollectList1();
        break
      case 2:
        that.getCollectList2();
        break
    }
  },

  //获取用户收藏商品列表
  getCollectList: function() {
    wx.showLoading({
      title: '商品加载中',
    })
    var that = this;
    wx.request({
      url: app.globalData.api + '/app.php?c=TbGoodsCollect&a=getCollectList',
      data: {
        token: app.globalData.token,
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success(res) {
        console.log(res)
        wx.hideLoading();
        // for (var i = 0; i < res.data.data.list.length; i++) {
        //   res.data.data.list[i].pict_url = app.globalData.imgUrl + "" + res.data.data.list[i].pict_url;
        // }
        if (res.data.data.list.length > 0) {
          that.setData({
            collectList: res.data.data.list,
            noData: false
          })
          console.log(that.data.collectList)
        } else {
          that.setData({
            noData: true
          })
        }
      },
      fail() {
        wx.hideLoading();
      }
    })
  },


  //获取用户收藏商品列表
  getCollectList1: function () {
    wx.showLoading({
      title: '商品加载中',
    })
    var that = this;
    wx.request({
      url: app.globalData.api + '/app.php?c=JingdongCollect&a=getCollectList',
      data: {
        token: app.globalData.token,
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success(res) {
        console.log(res)
        wx.hideLoading();
        if (res.data.data.list.length > 0) {
          that.setData({
            collectList1: res.data.data.list,
            noData: false
          })
          console.log(that.data.collectList)
        } else {
          that.setData({
            noData: true
          })
        }
      },
      fail() {
        wx.hideLoading();
      }
    })
  },

  //获取用户收藏商品列表
  getCollectList2: function () {
    wx.showLoading({
      title: '商品加载中',
    })
    var that = this;
    wx.request({
      url: app.globalData.api + '/app.php?c=PddCollect&a=getCollectList',
      data: {
        token: app.globalData.token,
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success(res) {
        console.log(res)
        wx.hideLoading();
        if (res.data.data.list.length > 0) {
          that.setData({
            collectList2: res.data.data.list,
            noData: false
          })
          console.log(that.data.collectList)
        } else {
          that.setData({
            noData: true
          })
        }
      },
      fail() {
        wx.hideLoading();
      }
    })
  },
 
  goodsDetail: function(e) {
    console.log(e)
    wx.navigateTo({
      url: '/pages/shop/goods-item?id=' + e.currentTarget.id,
    })
  },


  //取消收藏
  deleteList: function(e) {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确定要取消收藏吗？',
      success: function(sm) {
        if (sm.confirm) {
          // 用户点击了确定 可以调用删除方法了
          wx.request({
            url: app.globalData.api + '/app.php?c=TbGoodsCollect&a=cancelCollect',
            data: {
              token: app.globalData.token,
              goods_id: e.currentTarget.id,
            },
            header: {
              "Content-Type": "application/x-www-form-urlencoded",
              'platform': '' + app.globalData.platform,
              'version': '' + app.globalData.version,
            },
            method: 'POST',
            dataType: 'json',
            success(res) {
              console.log(res)
              if (res.data.code == 0) {
                that.setData({
                  is_collect: false
                })
                wx.showToast({
                  title: '取消收藏成功',
                  icon: 'success',
                  duration: 1000
                })

                setTimeout(function() {
                  //要延时执行的代码
                  that.getCollectList();
                }, 1000) //延迟时间 这里是1秒

              } else {
                wx.showToast({
                  title: '' + res.data.msg,
                  icon: 'none',
                  duration: 1000
                })
              }
            }
          })
        } else if (sm.cancel) {

        }
      }
    })
  }
});